package net.xiduth.game.player.content.citadels;

import net.xiduth.game.Animation;
import net.xiduth.game.Graphics;
import net.xiduth.game.WorldObject;
import net.xiduth.game.WorldTile;
import net.xiduth.game.player.controlers.Controler;
import net.xiduth.game.tasks.WorldTask;
import net.xiduth.game.tasks.WorldTasksManager;

/**
 * Controls the Clan citadel while a player is inside
 * 
 * @author Jae
 * 
 */

public class ClanCitadel extends Controler {

	@Override
	public void start() {

	}

	protected static final int HOME_ANIMATION = 16385;

	protected static final int HOME_GRAPHIC = 3017;

	protected static final int DONE_ANIMATION = 16386;

	/**
	 * normal home tele, but teles to the citadle base.
	 */
	@Override
	public boolean processMagicTeleport(WorldTile toTile) {
		WorldTasksManager.schedule(new WorldTask() {
			int loop;

			@Override
			public void run() {
				if (loop == 0) {
					player.setNextAnimation(new Animation(HOME_ANIMATION));
					player.setNextGraphics(new Graphics(HOME_GRAPHIC));
				} else if (loop == 10) {
					player.setNextWorldTile(new WorldTile(
							Citadel.getBaseX() + 242, Citadel.getBaseY() + 40,
							0));
					player.setNextAnimation(new Animation(HOME_ANIMATION + 1));
					player.setNextGraphics(new Graphics(HOME_GRAPHIC + 1));
				} else if (loop == 13) {
					player.setNextAnimation(new Animation(-1));
					player.setNextGraphics(new Graphics(-1));
				}

				loop++;
			}
		}, 0, 1);
		return false;
	}

	/**
	 * process denied
	 */

	@Override
	public boolean processItemTeleport(WorldTile toTile) {
		player.getDialogueManager().startDialogue("SimpleMessage",
				"Use the home teleport!");
		return false;

	}

	@Override
	public boolean processObjectClick1(final WorldObject object) {
		if (object.getId() == 59462) {
			player.getDialogueManager().startDialogue("LeavingCitadel");
			return false;
		}
		if (object.getId() == 58847) {
			player.setNextWorldTile(new WorldTile(player.getX(),
					player.getY() + 5, 0));
			return false;
		}
		if (object.getId() == 59464) {
			player.getInterfaceManager().sendInterface(1098);
			return false;
		}
		return true;
	}

	@Override
	public boolean processObjectClick2(final WorldObject object) {
		if (object.getId() == 59462) {
			// TODO 2nd option for portal
			return false;
		}
		if (object.getId() == 59464) {
			return false;
		}
		return true;
	}

	@Override
	public void forceClose() {
	}

	@Override
	public boolean sendDeath() {
		WorldTasksManager.schedule(new WorldTask() {
			int loop;

			@Override
			public void run() {
				player.stopAll();
				if (loop == 0) {
					player.setNextAnimation(new Animation(836));
				} else if (loop == 1) {
					player.getPackets().sendGameMessage(
							"Oh dear, you have died.");
				} else if (loop == 3) {
					player.setNextAnimation(new Animation(-1));
					player.reset();
					player.setCanPvp(false);
					player.setNextWorldTile(new WorldTile(
							Citadel.getBaseX() + 242, Citadel.getBaseY() + 40,
							0));
					stop();
				}
				loop++;
			}
		}, 0, 1);
		return false;
	}

	/*
	 * logging in while they didn't leave.
	 */

	@Override
	public boolean login() {
		Citadel.LeaveCitadel(player);
		return false;

	}

	@Override
	public boolean logout() {
		return true;
	}

}
